<?php 
	class User_visit_statistics extends CI_Model{

		function __construct(){
			parent::__construct();
		}

		function get_visit_info($user_id = 0, $conditions = array(), $limit = 0, $offset = 0){
			$where = " 1=1 ";
			if(!empty($user_id)){
				$where .= " AND user_id = '{$user_id}' ";
			}
			if(!empty($conditions['start_time']) || !empty($conditions['end_time'])){
				if(!empty($conditions['start_time'])){
					$time = strtotime($conditions['start_time']);
					$where .= " AND last_time >= '{$time}'";
				}
				if(!empty($conditions['end_time'])){
					$time = (strtotime($conditions['end_time']) + 3600 * 24);
					$where .= " AND last_time < '{$time}'";
				}
			}elseif(!empty($conditions['statistics_type'])){
				//统计本周
				if($conditions['statistics_type'] == 1){
					$time = strtotime(date('Y-m-d', time())) - date('w',strtotime(date('Y-m-d', time())))*86400 + 3600 * 24;
				}elseif($conditions['statistics_type'] == 2){
					//统计本月
					$time = mktime(0,0,0,date('m'),1,date('Y'));
				}
				$where .= "AND last_time >= $time";
			}
			$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}user_visit_statistics WHERE $where ORDER BY id DESC ";
			if($limit >0 || $offset > 0){
				$sql .= " LIMIT $limit, $offset";
			}
			$result = $this->db->query($sql)->result_array();
			$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
			return array("count" => $row_num['row_num'], "result" => $result);
		}
	}